home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / SOURCE.BIN / List.java < prev    next >
Encoding:
Java Source  |  1997-06-19  |  29.9 KB  |  669 lines

  1. /*
  2.  * 17    1/24/97 4:27p Tjones
  3.  * Removed 'throws SQLException' from setbinding method
  4.  *
  5.  * 16    1/17/97 3:14p Tjones
  6.  * Modified setBinding method to throw SQLException
  7.  *
  8.  * 15    12/19/96 1:42p Tjones
  9.  * Bug fix with the setTreatBlankAs method
  10.  *
  11.  * 14    12/18/96 2:53p Tjones
  12.  * Minor cleanup of unused variables and stuff.
  13.  *
  14.  * 13    12/16/96 1:50p Cthrons
  15.  * Moved list binding code from Applet into component.
  16.  * Make static list and lookup work together.
  17.  * Call raiseException instead of System.out.println.
  18.  * Call makeVisible in setText.
  19.  *
  20.  * 12    12/16/96 9:54a Tjones
  21.  * Added to parameter list for call to setValueFromString.
  22.  * Add setTreatBlankAs method.
  23.  *
  24.  * 11    12/08/96 1:30a Cthrons
  25.  * Maintain and use m_IsBound variable.
  26.  * Pass ProjBinder to notifyInputChanged.
  27.  *
  28.  * 10    12/08/96 12:08a Cthrons
  29.  * Copied standard proj link methods from TextField
  30.  *
  31.  * 9     12/07/96 6:17p Tjones
  32.  * Changed return type of notifySetData to boolean.
  33.  *
  34.  * 8     11/27/96 5:02p Kbradle
  35.  * Add the notifySetData() methods, currently just stubs for this
  36.  * listbinding component.
  37.  *
  38.  * 7     11/25/96 2:29p Tjones
  39.  *
  40.  * 6     11/25/96 1:40p Tjones
  41.  *
  42.  * 5     11/25/96 1:36p Tjones
  43.  * Introduced dynamic edit method
  44.  *
  45.  * 4     11/14/96 8:22p Cthrons
  46.  * Put in safety code to make sure binder is readable, writable, etc...
  47.  *
  48.  * 3     11/01/96 1:41p Cthrons
  49.  * Implement ProjLink interface.
  50.  * Process key input.
  51.  *
  52.  * 2     10/29/96 1:49p Cthrons
  53.  * Changed the package name.
  54.  * Fixed up notifyListChanged so that it would compile.
  55.  *
  56.  * 1     9/25/96 5:17p Spollac
  57.  * an object that implements the ListLink interface
  58. */
  59.  
  60. package symantec.itools.db.awt;
  61.  
  62. import java.util.*;
  63. import symjava.sql.*;
  64. import java.lang.*;
  65. import symantec.itools.db.net.*;
  66. import symantec.itools.db.pro.*;
  67.  
  68. public class List extends java.awt.List implements ListLink, ProjLink
  69. {
  70.  
  71.     /**
  72.      * The  Binder object which this component can use to get data from
  73.      * the server, or change data at the database server.
  74.      */
  75.     private ListBinder   m_ListBinder;
  76.  
  77.     /**
  78.      * The ProjBinder object which this component can use to get data from
  79.      * the server, or change data at the database server.
  80.      */
  81.     private ProjBinder m_ProjBinder;
  82.  
  83.     private boolean m_DynamicUpdate = false;
  84.     private String  m_BinderData;    // Keep in synch with database
  85.     private String  m_ScreenData;    // Keep in synch with screen text
  86.     private boolean m_useStream = false;
  87.     private boolean m_IsBound = false;
  88.     private int     m_treatBlankAs = 0;
  89.  
  90.     private RelationView    m_LookUpRelationView;
  91.     private Vector          m_JoinColumns;
  92.     private ConnectionInfo  m_Conn;
  93.     private String          m_SQL;
  94.  
  95.     //*************************************************************************//
  96.     //                                                                         //
  97.     // FUNCTION: List                                                          //
  98.     //                                                                         //
  99.     //  PURPOSE:                                                               //
  100.     //                                                                         //
  101.     //  PARAMETERS:                                                            //
  102.     //                                                                         //
  103.     //  RETURNS:                                                               //
  104.     //                                                                         //
  105.     // COMMENTS:                                                               //
  106.     //                                                                         //
  107.     //*************************************************************************//
  108.  
  109.     public List(RelationView lookupRV, ConnectionInfo conn)
  110.     {
  111.         this();
  112.         m_LookUpRelationView = lookupRV;
  113.         m_Conn = conn;
  114.     }
  115.  
  116.     /**
  117.      * Constructs a new List with no data connectivity, initially.
  118.      *
  119.      * @param rows defines the number of rows of items to display.
  120.      */
  121.     public List(int rows)
  122.     {
  123.         super(rows, false);
  124.         m_ScreenData = new String();
  125.         m_BinderData = new String();
  126.         m_JoinColumns = new Vector();
  127.         m_SQL = new String();
  128.     }
  129.  
  130.     /**
  131.      * Constructs a new List with no data connectivity, initially.
  132.      */
  133.     public List()
  134.     {
  135.         this(0);
  136.     }
  137.  
  138.     /**
  139.      * Implement the  ProjLink interface function, init().
  140.      * This function is called when the control is first bound to data.
  141.      *
  142.      * @param binder The  Binder binder object which this component can use
  143.      *               to change data at the database server.
  144.      */
  145.     public void init (ListBinder lBinder)
  146.     {
  147.         m_ListBinder = lBinder;
  148.     }
  149.  
  150.     /**
  151.      * Implement the  ProjLink interface function, notifyDataChange().
  152.      * This function is called when the projection's data changes at the database.
  153.      *
  154.      * @param binder The  Binder binder object which this component can use
  155.      *               to get or change data at the database server.
  156.      */
  157.     public void notifyListChange(ListBinder lBinder)
  158.     {
  159.         try
  160.         {
  161.             RelationView curRV = lBinder.getRelationView();
  162.             clear();
  163.             while ( curRV.next() == true )
  164.             {
  165.                 if (!(curRV.isNull(1)))
  166.                 {
  167.                     super.addItem( curRV.getString(1));
  168.                 }
  169.             }
  170.             Enumeration e = staticItems.elements();
  171.             while(e.hasMoreElements())
  172.             {
  173.                 super.addItem((String)e.nextElement());
  174.             }
  175.             if (m_IsBound)
  176.             {
  177.                 notifyDataChange(m_ProjBinder);
  178.             }
  179.         }
  180.         catch (SQLException Ex)
  181.         {
  182.             raiseException ("Exception from List.notifyDataChange.setString: " + Ex.getMessage());
  183.         }
  184.     }
  185.  
  186.     //*************************************************************************//
  187.     //                                                                         //
  188.     // FUNCTION: init                                                          //
  189.     //                                                                         //
  190.     //  PURPOSE:                                                               //
  191.     //                                                                         //
  192.     // PARAMETERS:                                                             //
  193.     //  @param binder The ProjBinder binder object which this component can use//
  194.     //                to change data at the database server.                   //
  195.     //                                                                         //
  196.     //  RETURNS:                                                               //
  197.     //                                                                         //
  198.     // COMMENTS:                                                               //
  199.     //  Implement the ProjLink interface function, init().                     //
  200.     //  This function is called when the control is first bound to data.       //
  201.     //                                                                         //
  202.     //*************************************************************************//
  203.  
  204.     public void init (ProjBinder binder)
  205.     {
  206.         m_ProjBinder = binder;
  207.         setEditable(m_ProjBinder);
  208.     }
  209.  
  210.     public void setTreatBlankAs(String blank)
  211.     {
  212.         if (new String(blank).toUpperCase().equals("DEFAULT"))
  213.         {
  214.             m_treatBlankAs = RelationView.SETBLANKTODEFAULT;
  215.         }
  216.         else if (new String(blank).toUpperCase().equals("NULL"))
  217.             {
  218.                 m_treatBlankAs = RelationView.SETBLANKTONULL;
  219.             }
  220.             else if (new String(blank).toUpperCase().equals("BLANK"))
  221.             {
  222.                 m_treatBlankAs = RelationView.SETBLANKTOEMPTY;
  223.             }
  224.     }
  225.  
  226.     //*************************************************************************//
  227.     //                                                                         //
  228.     // FUNCTION: setBinding                                                    //
  229.     //                                                                         //
  230.     //  PURPOSE:                                                               //
  231.     //                                                                         //
  232.     // PARAMETERS:                                                             //
  233.     //                                                                         //
  234.     //  RETURNS:                                                               //
  235.     //                                                                         //
  236.     // COMMENTS:                                                               //
  237.     //                                                                         //
  238.     //*************************************************************************//
  239.  
  240.     public void setBinding(RelationView relView, String projection)
  241.     {
  242.         try
  243.         {
  244.             int projectionNumber = relView.findProjByName(projection);
  245.             relView.bindProj(projectionNumber, this);
  246.         }
  247.         catch (SQLException Ex)
  248.         {
  249.             raiseException(
  250.                 "SQLException from TextField.notifyDataChange.getStringValue: "
  251.                 + Ex.getMessage());
  252.             return;
  253.         }
  254.         m_IsBound = true;
  255.     }
  256.  
  257.     //*************************************************************************//
  258.     //                                                                         //
  259.     // FUNCTION: setDynamicUpdate                                              //
  260.     //                                                                         //
  261.     //  PURPOSE:                                                               //
  262.     //                                                                         //
  263.     // PARAMETERS:                                                             //
  264.     //                                                                         //
  265.     //  RETURNS:                                                               //
  266.     //                                                                         //
  267.     // COMMENTS:                                                               //
  268.     //                                                                         //
  269.     //*************************************************************************//
  270.  
  271.     public void setDynamicUpdate(boolean update)
  272.     {
  273.         m_DynamicUpdate = update;
  274.     }
  275.  
  276.     //*************************************************************************//
  277.     //                                                                         //
  278.     // FUNCTION: notifyDataChange                                              //
  279.     //                                                                         //
  280.     //  PURPOSE:                                                               //
  281.     //    Implement the ProjLink interface function, notifyDataChange().       //
  282.     //    This function is called when the projection's data changes at        //
  283.     //    the database.                                                        //
  284.     //                                                                         //
  285.     // PARAMETERS:                                                             //
  286.     //  @param binder The ProjBinder binder object which this component can use//
  287.     //                to get or change data at the database server.            //
  288.     //                                                                         //
  289.     //  RETURNS:                                                               //
  290.     //                                                                         //
  291.     // COMMENTS:                                                               //
  292.     //                                                                         //
  293.     //  Step 1: set m_ProjBinder equal to binder object                        //
  294.     //  Step 2: synchronize m_BinderData with binder object                    //
  295.     //  Step 3: synchronize m_ScreenData with current screen                   //
  296.     //  Step 4: if the two are different then                                  //
  297.     //       - set m_ScreenData equal to m_BinderData to avoid recursion       //
  298.     //       - call setText to update screen                                   //
  299.     //  Step 5: enable or disable editing based on binder                      //
  300.     //                                                                         //
  301.     //*************************************************************************//
  302.  
  303.     public void notifyDataChange(ProjBinder binder)
  304.     {
  305.         // STEP 1
  306.         if (binder == null)
  307.         {
  308.             // Raise an exception..
  309.             return;
  310.         }
  311.         m_ProjBinder = binder;
  312.  
  313.         // STEP 2
  314.         try
  315.         {
  316.             if (binder.getRelationView().getCurrentRecordState()
  317.                 != Record.RECSTATE_INVALID)
  318.             {
  319.                 if (binder.isReadable() && !binder.isNull())
  320.                 {
  321.                     m_BinderData = binder.getStringValue();
  322.                 }
  323.                 else
  324.                 {
  325.                     m_BinderData = "";
  326.                 }
  327.             }
  328.             else
  329.             {
  330.                 m_BinderData = "";
  331.             }
  332.         }
  333.         catch (SQLException Ex)
  334.         {
  335.             raiseException(
  336.                 "SQLException from TextField.notifyDataChange.getStringValue: "
  337.                 + Ex.getMessage());
  338.         }
  339.         catch (java.io.IOException Ex)
  340.         {
  341.             raiseException(
  342.                 "IOException from TextField.notifyDataChange.getStringValue: "
  343.                 + Ex.getMessage());
  344.         }
  345.         if (m_BinderData == null)
  346.         {
  347.             m_BinderData = new String();
  348.         }
  349.  
  350.         // STEP 3
  351.         m_ScreenData = getText();
  352.  
  353.         // STEP 4
  354.         if (!m_BinderData.equals(m_ScreenData))
  355.         {
  356.             m_ScreenData = m_BinderData;
  357.             setText(m_ScreenData);
  358.         }
  359.  
  360.         // STEP 5
  361.         setEditable(m_ProjBinder);
  362.     }
  363.  
  364.     //*************************************************************************//
  365.     //                                                                         //
  366.     // FUNCTION: lostFocus                                                     //
  367.     //                                                                         //
  368.     //  PURPOSE: Intercept the event to send data to the binder                //
  369.     //                                                                         //
  370.     // PARAMETERS:                                                             //
  371.     //      @param evt the lost focus event being caught                       //
  372.     //      @param what the object that is losing focus                        //
  373.     //                                                                         //
  374.     //  RETURNS:                                                               //
  375.     //                                                                         //
  376.     // COMMENTS:                                                               //
  377.     //      Call notifySetData since that is the standard way of handling      //
  378.     //      any event that should cause us to send data to the binder.         //
  379.     //                                                                         //
  380.     //*************************************************************************//
  381.  
  382.     public boolean lostFocus(java.awt.Event evt, Object what)
  383.     {
  384.         if (m_IsBound)
  385.         {
  386.             notifySetData(m_ProjBinder);
  387.         }
  388.         return super.lostFocus(evt, what);
  389.     }
  390.  
  391.     //*************************************************************************//
  392.     //                                                                         //
  393.     // FUNCTION: notifyInputChange                                             //
  394.     //                                                                         //
  395.     //  PURPOSE: Handle any event that causes us to send data to binder object //
  396.     //                                                                         //
  397.     //  PARAMETERS:                                                            //
  398.     //                                                                         //
  399.     //  RETURNS:                                                               //
  400.     //                                                                         //
  401.     // COMMENTS:                                                               //
  402.     //                                                                         //
  403.     //                                                                         //
  404.     //  Step 1: synchronize m_ScreenData with current screen                   //
  405.     //  Step 2: if m_ScreenData differs from m_BinderData then...              //
  406.     //       - set m_BinderData equal to m_ScreenData to avoid recursion       //
  407.     //       - call m_Binder.setString to update binder                        //
  408.     //                                                                         //
  409.     //*************************************************************************//
  410.  
  411.     boolean notifyInputChanged(ProjBinder binder, String input)
  412.     {
  413.         // STEP 1
  414.         if (!m_ScreenData.equals(input))
  415.         {
  416.             m_ScreenData = input;
  417.         }
  418.  
  419.         // STEP 2
  420.         if (!m_BinderData.equals(m_ScreenData))
  421.         {
  422.             m_BinderData = m_ScreenData;
  423.             try
  424.             {
  425.                 binder.setValueFromString(m_ScreenData, 0, m_treatBlankAs);
  426.             }
  427.             catch (SQLException Ex)
  428.             {
  429.                 raiseException(
  430.                     "SQLException from List.notifyInputChange: "
  431.                     + Ex.getMessage());
  432.                 return false;
  433.             }
  434.             catch (java.io.IOException Ex)
  435.             {
  436.                 raiseException(
  437.                     "IOException from List.notifyInputChange: "
  438.                     + Ex.getMessage());
  439.                 return false;
  440.             }
  441.         }
  442.         return true;
  443.     }
  444.  
  445.     //*************************************************************************//
  446.     //                                                                         //
  447.     // FUNCTION: notifySetData                                                 //
  448.     //                                                                         //
  449.     //  PURPOSE: Send data to binder                                           //
  450.     //                                                                         //
  451.     //  PARAMETERS:                                                            //
  452.     //  @param binder The ProjBinder binder object which this component can use//
  453.     //                to change data at the database server.                   //
  454.     //                                                                         //
  455.     //  RETURNS:                                                               //
  456.     //                                                                         //
  457.     // COMMENTS:                                                               //
  458.     //                                                                         //
  459.     //                                                                         //
  460.     //*************************************************************************//
  461.  
  462.     public boolean notifySetData(ProjBinder binder)
  463.     {
  464.         return ( notifyInputChanged(binder, getText()) );
  465.     }
  466.  
  467.     //*************************************************************************//
  468.     //                                                                         //
  469.     // FUNCTION: handleEvent                                                   //
  470.     //                                                                         //
  471.     //  PURPOSE: Respond to any event we want to handle ourselves.             //
  472.     //                                                                         //
  473.     //  PARAMETERS:                                                            //
  474.     //                                                                         //
  475.     //  RETURNS:                                                               //
  476.     //                                                                         //
  477.     // COMMENTS:                                                               //
  478.     // * Handles the event. Returns true if the event is handled and           //
  479.     // * should not be passed to the parent of this component. The default     //
  480.     // * event handler calls some helper methods to make life easier           //
  481.     // * on the programmer.                                                    //
  482.     // * @param evt the event                                                  //
  483.     // * @;see #mouseEnter                                                     //
  484.     // * @see #mouseExit                                                       //
  485.     // * @see #mouseMove                                                       //
  486.     // * @see #mouseDown                                                       //
  487.     // * @see #mouseDrag                                                       //
  488.     // * @see #mouseUp                                                         //
  489.     // * @see #keyDown                                                         //
  490.     // * @see #action                                                          //
  491.     //                                                                         //
  492.     //*************************************************************************//
  493.  
  494.     public boolean handleEvent(java.awt.Event evt) {
  495.         if (evt.id == java.awt.Event.LIST_SELECT && m_IsBound)
  496.         {
  497.             notifySetData(m_ProjBinder);
  498.         }
  499.         return super.handleEvent(evt);
  500.     }
  501.  
  502.     void setText(String text)
  503.     {
  504.         boolean bitemFound = false;
  505.         for (int index = 0; index < countItems(); index++)
  506.         {
  507.             if (text.equals(getItem(index)))
  508.             {
  509.                 select(index);
  510.                 makeVisible(index);
  511.                 bitemFound = true;
  512.                 break;
  513.             }
  514.             if (!bitemFound)
  515.             {
  516.                 if (getSelectedIndex() != -1)
  517.                 {
  518.                     deselect(getSelectedIndex());
  519.                 }
  520.             }
  521.         }
  522.     }
  523.  
  524.     //*************************************************************************//
  525.     //                                                                         //
  526.     // FUNCTION: setEditable                                                   //
  527.     //                                                                         //
  528.     //  PURPOSE: Enable or disable input                                       //
  529.     //                                                                         //
  530.     //  PARAMETERS:                                                            //
  531.     //  @param binder The ProjBinder binder object which this component can use//
  532.     //                to change data at the database server.                   //
  533.     //                                                                         //
  534.     //  RETURNS:                                                               //
  535.     //                                                                         //
  536.     // COMMENTS:                                                               //
  537.     //                                                                         //
  538.     //*************************************************************************//
  539.  
  540.     void setEditable(ProjBinder binder)
  541.     {
  542.         //Disable input if binder is not writeable
  543.         boolean isWritable = false;
  544.  
  545.         try
  546.         {
  547.             if (binder != null)
  548.             {
  549.                 RelationView rv = binder.getRelationView();
  550.                 if (rv != null && (rv.getCurrentRecordState() != Record.RECSTATE_INVALID))
  551.                 {
  552.                     isWritable = binder.isWritable();
  553.                 }
  554.             }
  555.         }
  556.         catch (SQLException Ex)
  557.         {
  558.             raiseException(Ex.getMessage());
  559.         }
  560.         // setEditable(isWritable);
  561.     }
  562.  
  563.     String getText()
  564.     {
  565.         String[] selectedItems = getSelectedItems();
  566.         if (selectedItems.length == 1)
  567.         {
  568.             return selectedItems[0];
  569.         }
  570.         return "";
  571.     }
  572.  
  573.     //*************************************************************************//
  574.     //                                                                         //
  575.     // FUNCTION: raiseException                                                //
  576.     //                                                                         //
  577.     //  PURPOSE:                                                               //
  578.     //                                                                         //
  579.     //  PARAMETERS:                                                            //
  580.     //                                                                         //
  581.     //  RETURNS:                                                               //
  582.     //                                                                         //
  583.     // COMMENTS:                                                               //
  584.     //                                                                         //
  585.     //*************************************************************************//
  586.  
  587.     void raiseException(String text)
  588.     {
  589.         System.out.println(text);
  590.     }
  591.  
  592.     //*************************************************************************//
  593.     //                                                                         //
  594.     // FUNCTION: join                                                          //
  595.     //                                                                         //
  596.     //  PURPOSE:                                                               //
  597.     //                                                                         //
  598.     //  PARAMETERS:                                                            //
  599.     //                                                                         //
  600.     //  RETURNS:                                                               //
  601.     //                                                                         //
  602.     // COMMENTS:                                                               //
  603.     //                                                                         //
  604.     //*************************************************************************//
  605.  
  606.     public void join(String columnName) throws SQLException
  607.     {
  608.         int colID = m_LookUpRelationView.findProjByName(columnName);
  609.         if (m_JoinColumns == null)
  610.         {
  611.             m_JoinColumns = new Vector();
  612.         }
  613.         m_JoinColumns.addElement(new Integer(colID));
  614.     }
  615.  
  616.     //*************************************************************************//
  617.     //                                                                         //
  618.     // FUNCTION: setSQL                                                        //
  619.     //                                                                         //
  620.     //  PURPOSE:                                                               //
  621.     //                                                                         //
  622.     //  PARAMETERS:                                                            //
  623.     //                                                                         //
  624.     //  RETURNS:                                                               //
  625.     //                                                                         //
  626.     // COMMENTS:                                                               //
  627.     //                                                                         //
  628.     //*************************************************************************//
  629.  
  630.     public void setSQL(String sql) throws SQLException
  631.     {
  632.         m_SQL = sql;
  633.     }
  634.  
  635.     //*************************************************************************//
  636.     //                                                                         //
  637.     // FUNCTION: bind                                                          //
  638.     //                                                                         //
  639.     //  PURPOSE:                                                               //
  640.     //                                                                         //
  641.     //  PARAMETERS:                                                            //
  642.     //                                                                         //
  643.     //  RETURNS:                                                               //
  644.     //                                                                         //
  645.     // COMMENTS:                                                               //
  646.     //                                                                         //
  647.     //*************************************************************************//
  648.  
  649.     public void bindList() throws SQLException
  650.     {
  651.         m_LookUpRelationView.bindList(m_Conn, m_SQL, this, m_JoinColumns);
  652.     }
  653.  
  654.  
  655.     /**
  656.      * Adds the specified item to the end of scrolling list.
  657.      * @param item the item to be added
  658.      */
  659.     public synchronized void addItem(String item) {
  660.         super.addItem(item, -1);
  661.         staticItems.addElement(item);
  662.         if (m_IsBound)
  663.         {
  664.             notifyDataChange(m_ProjBinder);
  665.         }
  666.     }
  667.  
  668.     private Vector staticItems = new Vector();
  669. }